<template>
  <div id="detail">
    <detail-nav-bar @titleClick="titleClick" ref="nav"/>
    <scroll class="content" ref="scroll" @scroll="contentScroll" :data="[topImages, goods, shop, detailInfo]"
            :probe-type="3">
      <div>
        <detail-swiper ref="base" :images="topImages"></detail-swiper>
        <detail-base-info :goods="goods"></detail-base-info>
        <detail-shop-info :shop="shop"></detail-shop-info>
        <detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad"></detail-goods-info>
        <detail-param-info ref="param" :param-info="paramInfo"></detail-param-info>
        <detail-comment-info ref="comment" :comment-info="commentInfo"></detail-comment-info>
        <detail-recommend-info ref="recommend" :recommend-list="recommendList"></detail-recommend-info>
      </div>
    </scroll>
    <detail-bottom-bar></detail-bottom-bar>
    <back-top @backTop="backTop" class="back-top" v-show="showBackTop">
      <img src="~assets/img/common/top.png" alt="">
    </back-top>
  </div>
</template>

<script>
  import Scroll from 'common/scroll/Scroll';
  import {getDetail, getRecommend, Goods, Shop, GoodsParam} from 'network/detail';

  import DetailNavBar from './childComps/DetailNavBar';
  import DetailSwiper from './childComps/DetailSwiper';
  import DetailBaseInfo from './childComps/DetailBaseInfo';
  import DetailShopInfo from './childComps/DetailShopInfo';
  import DetailGoodsInfo from './childComps/DetailGoodsInfo';
  import DetailParamInfo from './childComps/DetailParamInfo';
  import DetailCommentInfo from './childComps/DetailCommentInfo';
  import DetailRecommendInfo from './childComps/DetailRecommendInfo';
  import DetailBottomBar from './childComps/DetailBottomBar'
  import {itemListenerMixin,backTopMixin} from '@/common/mixin';
  import {debounce} from "../../common/utils";
  import BackTop from 'content/backTop/BackTop'
  import {
    BACKTOP_DISTANCE
  } from '@/common/const'
  export default {
    name: 'Detail',
    data() {
      return {
        topImages: [],
        iid: '',
        goods: {},
        shop: {},
        detailInfo: {},
        paramInfo: {},
        commentInfo: {},
        recommendList: [],
        themeTops: [],
        _getOffsetTops: null,
        currentIndex: 0
      };
    },
    components: {
      DetailNavBar,
      DetailSwiper,
      DetailBaseInfo,
      DetailShopInfo,
      Scroll,
      DetailGoodsInfo,
      DetailParamInfo,
      DetailCommentInfo,
      DetailRecommendInfo,
      DetailBottomBar,
      BackTop
    },
    mixins: [itemListenerMixin,backTopMixin],
    created() {
      this._getDetailData();
      this._getRecommend();
      this._getOffsetTops = debounce(() => {
        this.themeTops = []
        this.themeTops.push(this.$refs.base.$el.offsetTop)
        this.themeTops.push(this.$refs.param.$el.offsetTop)
        this.themeTops.push(this.$refs.comment.$el.offsetTop)
        this.themeTops.push(this.$refs.recommend.$el.offsetTop)
        this.themeTops.push(Number.MAX_VALUE)
      }, 100);
    },
    updated() {
      // 获取需要的四个offsetTop
      this._getOffsetTops()
    },
    methods: {
      titleClick(index) {
        this.$refs.scroll.scrollTo(0, -this.themeTops[index], 200)
      },
      contentScroll(position) {
        // 1.监听backTop的显示
        this.showBackTop = position.y < -BACKTOP_DISTANCE

        const posY = -position.y;
        //主题中值进行对比
        let length = this.themeTops.length;
        for (let i = 0; i < length; i++) {
          let iPos = this.themeTops[i];
          if (posY >= iPos && posY < this.themeTops[i + 1]) {
            if (this.currentIndex !== i) {
              this.currentIndex = i;
            }
            break;
          }
        }
        this.$refs.nav.currentIndex = this.currentIndex;
      },
      /**
       * 获取详情数据
       */
      _getDetailData() {
        const iid = this.$route.query.iid;
        this.iid = iid;
        getDetail(iid).then(res => {
          //1.获取顶部轮播数据
          const data = res.result;

          // 2.2.获取顶部信息
          this.topImages = data.itemInfo.topImages;

          //2.3获取goods信息
          this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services);

          //2.4获取商户信息
          this.shop = new Shop(data.shopInfo);

          // 2.5.获取商品信息
          this.detailInfo = data.detailInfo;

          // 2.6.保存参数信息
          this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule);

          //2.7评论信息
          if (data.rate.cRate != 0) {
            this.commentInfo = data.rate.list[0];
          }
          //界面渲染完成回调,但是图片依旧没有加载完
          // this.$nextTick(()=>{
          //
          // })
        });
      },
      _getRecommend() {
        getRecommend().then((res, error) => {
          if (error) return
          this.recommendList = res.data.list
        })
      },
      /**
       * 监听图片加载完成事件
       */
      imageLoad() {
        this.newRefresh();
      }
    }
  };
</script>

<style scoped>
  #detail {
    height: 100vh;
    position: relative;
    z-index: 1;
    background-color: #fff;
  }

  .content {
    position: absolute;
    top: 44px;
    bottom: 60px;
  }
  .back-top {
    position: absolute;
    right: 10px;
    bottom: 65px;
  }
</style>
